﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;

namespace BLL
{
    public class OrderBase
    {
        #region 添加 \ 修改订单
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="_complaint"></param>
        /// <returns></returns>
        public static string OrderCreate(Model.OrderBase _order)
        {
            _order.OrderNumber = BuildOrderNumber();

            return (new SQLServerDAL.OrderBase()).OrderAdmin(_order);
        }
        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="_complaint"></param>
        /// <returns></returns>
        public static string OrderModify(Model.OrderBase _order)
        {
            return (new SQLServerDAL.OrderBase()).OrderAdmin(_order);
        }
        #endregion

        #region 生成订单编号
        /// <summary>
        /// 生成订单编号
        /// </summary>
        /// <returns></returns>
        private static string BuildOrderNumber()
        {
            string billnumber = "";

            int counter = 0;

            object _obj = BLL.CommonBLL.ExecuteScalar("OrderBase", "COUNT(1)", " AND DATEDIFF(DAY, GETDATE(), CreatedOn) = 0");
            if (_obj != null)
            {
                counter = (int)_obj;
            }

            counter++;

            DateTime dtNow = DateTime.Now;
            string prefix = dtNow.Year.ToString() + dtNow.ToString("MM") + dtNow.ToString("dd");

            for (int i = 0; i < 3 - counter.ToString().Length; i++)
            {
                billnumber += "0";
            }
            billnumber += counter.ToString();

            return prefix + billnumber;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 匹配ID获取订单实体信息
        /// </summary>
        public static Model.OrderBase GetObjectById(string orderId, out string result)
        { 
            if (Utils.IsGuid(orderId))
            {
                result = "succeeded";
                return (new SQLServerDAL.OrderBase()).GetObjectById(orderId);
            }
            else
            {
                result = "输入参数错误!";
                return null;
            }
        }
        /// <summary>
        /// 匹配订单号获取订单实体信息
        /// </summary>
        public static Model.OrderBase GetObjectByOrderNumber(string ordernumber, string userid, out string result)
        {
            if (Utils.IsNumber(ordernumber) && Utils.IsGuid(userid))
            {
                result = "succeeded";
                return (new SQLServerDAL.OrderBase()).GetObjectByOrderNumber(ordernumber, userid);
            }
            else
            {
                result = "输入参数错误!";
                return null;
            }
        }
        /// <summary>
        /// 匹配 orderId 获取订单明细
        /// </summary>
        /// <param name="complaintId"></param>
        /// <returns></returns>
        public static IList<Model.OrderItems> GetItems(string orderId, out string result)
        {
            if (Utils.IsGuid(orderId))
            {
                result = "succeeded";
                return (new SQLServerDAL.OrderBase()).GetItems(orderId);
            }
            else
            {
                result = "输入参数错误!";
                return null;
            }
        }
        #endregion

        #region 设置
        /// <summary>
        /// 设置订单状态
        /// </summary>
        /// <param name="status"></param>
        /// <param name="productid"></param>
        /// <returns></returns>
        public static string CancelOrder(string orderid, string userid, int reasoncode, string reason)
        {
            return (new SQLServerDAL.OrderBase()).CancelOrder(orderid, userid, reasoncode, reason);
        }

        public static string ExchangeConfirm(string userId, string orderId, int integralCount)
        {
            return (new SQLServerDAL.OrderBase()).ExchangeConfirm(userId, orderId, integralCount); 
        }
        /// <summary>
        /// 修改订单状态
        /// </summary> 
        /// <returns></returns>
        public static string UpdateStatus(string ordernumber, string modifyby, int status)
        {
            return (new SQLServerDAL.OrderBase()).UpdateStatus(ordernumber, modifyby, status);  
        }
        #endregion 
    }
}
